<?php
session_start();
/////////////////////////////////////////////////////////////////////
// INCLUDES

/**
 * Add $relativePath to search/include path.
 *
 * @param string $relativePath - path to add
 */
function pathAddInclude($relativePath) {
	$path = getcwd() . '/' . $relativePath;
	set_include_path(get_include_path() . PATH_SEPARATOR . $path);
}
pathAddInclude('include');
pathAddInclude('include/src/base');
pathAddInclude('include/src/common');
pathAddInclude('include/src/db');
pathAddInclude('include/src/etc');
pathAddInclude('include/src/html');
pathAddInclude('include/src/site');

require_once 'include/_constants.php';
require_once 'include/src/base/_typehint.php';
require_once 'include/src/common/_logger.php';
require_once 'include/src/common/_xmlreaderex.php';
require_once 'include/src/site/_sitecontext.php';
require_once 'include/src/base/_codemanager.php';
require_once 'include/src/db/_dbconnector.php';

error_reporting(E_ALL & ~E_NOTICE);
require_once 'include/src/db/_dbobject.php';
require_once 'include/src/html/_htmlbase.php';
error_reporting(E_ALL | E_STRICT);
require_once 'include/src/html/_htmlhelper.php';
require_once 'include/src/html/_htmloffer.php';
require_once 'include/src/common/_img.php';
require_once 'include/src/base/_system.php';
require_once 'include/src/base/_redirector.php';
require_once 'include/src/common/_error.php';
require_once 'include/src/common/_stringex.php';
require_once 'include/src/base/_search.php';
require_once 'include/src/common/_definitions.php';
require_once 'include/src/html/_htmluseradmin.php';
require_once 'include/src/base/_localemanager.php';
require_once 'include/src/base/_ajaxhandle.php';
require_once 'include/src/site/_siteseo.php';
require_once 'include/src/site/_sitemap.php';

/////////////////////////////////////////////////////////////////////
// INITIALIZATION
System::init();
$info = SiteContext::getSiteContext();
$cm = CodeManager::getCodeManager($info);


Logger::setLevel(Logger::DEBUG_LEVEL);
$logger = Logger::getLogger();

/**
 * Process action
 */ 
if (!isset($_REQUEST[Constants::REQ_ACTION])) {
	$reqAction = '';
} else {
	$reqAction = $_REQUEST[Constants::REQ_ACTION];
}

try {
switch($reqAction) {
	case Constants::REQ_ACTION_REDIRECT:
		if (isset($_REQUEST[Constants::REQ_TYPE])) {
			Redirector::init($_REQUEST[Constants::REQ_TYPE]);
		} else {
			Error::getError()->addErrorMessage($cm->get("30216"));
		}
	break;
	case Constants::REQ_ACTION_AJAX:
		AjaxHandle::processAjaxCall();
		System::finalScriptExecute();
		die;
	break;
	case Constants::REQ_ACTION_SITEMAP:
		Sitemap::init();
		die;
	break;
	case Constants::REQ_ACTION_ERROR:
		header("HTTP/1.1 404 Not Found" );
		header("Status: 404 Not Found" );
	break;
}

/**
 * Redirector can influence a executing flow to the previous page.
 * Is used when invalid data from a form were sent.
 */
if (!is_null($newAction = Redirector::getRequiredHistoryBack())) {
	$reqAction = $newAction;
}

/** include header */
$info->getSiteInf()->templateHeader();


/** Decide what to print into the output */
switch ($reqAction) {
	case Constants::REQ_ACTION_REGISTATION:
		$info->getSiteInf()->templateRegistration();
	break;
	case Constants::REQ_ACTION_LOGIN:
		$info->getSiteInf()->templateLogin();
	break;
	case Constants::REQ_ACTION_OFFEREDIT:
		$info->getSiteInf()->templateOfferEdit();
	break;
	case Constants::REQ_ACTION_OFFER:
		$info->getSiteInf()->templateSearch(); 
		HtmlOffer::init();
	break;
	case Constants::REQ_ACTION_THUMBOFFER:
		$info->getSiteInf()->templateSearch(); 
		HtmlOfferBase::pickHtmlOfferInitializer(false, true);
	break;
	case Constants::REQ_ACTION_INTERESTEDIN:
		$info->getSiteInf()->templateSearch(); 
		HtmlOfferBase::pickHtmlOfferInitializer(Common::createQuerySpecForIdArray($info->getInterestedInItem()), true);
	break;
	case Constants::REQ_ACTION_SEARCH:
		$info->getSiteInf()->templateSearch(); 
		Search::init();
	break;
	case Constants::REQ_ACTION_USERLIST:
		HtmlUserAdmin::init();
	break;
	case Constants::REQ_ACTION_SETTINGS:
		$info->getSiteInf()->templateSettings();
	break;
	case Constants::REQ_ACTION_CONTACT:
		$info->getSiteInf()->templateCustom("contactPage", true);
	break;
	case Constants::REQ_ACTION_ABOUT:
		$info->getSiteInf()->templateCustom("aboutPage", true);
	break;
	case Constants::REQ_ACTION_SEO:
		SiteSeo::getSiteSeo()->handleSeoUrl();
	break;
	case Constants::REQ_ACTION_ERROR:
		Error::getError()->addErrorMessage($cm->get("30216"));
	default:
		$info->getSiteInf()->templateWelcomePage(); 
	break;
}

} catch (Exception $e) {
	/** Exception occured */
	try {
		$logger->error($e->getMessage());
		$logger->error($e->getTraceAsString());
		if ($e instanceof ExceptionDisplayable) {
			Error::getError()->addErrorMessage($e->getMessage());
		} else {
			Error::getError()->addErrorMessage(CodeManager::getCodeManager()->get("30211"));
		}
	} catch (Exception $e) {
		$logger->error($e->getMessage());
		$logger->error($e->getTraceAsString());
		echo "<h2>Critical error Occured.</h2>";
	}
}

try {
	$info->getSiteInf()->templateFooter();
	
	/** Execute last call */
	System::finalScriptExecute();
} catch (Exception $e) {
	$logger->error($e->getMessage());
	$logger->error($e->getTraceAsString());
}
?>
